﻿//
// GNS_SendMsgWnd.cpp
// GameHall
//
// Created by mr.l on 16/2/2.
//
//

#include "GNS_YJFLayer.h"


#define winsize Director::getInstance()->getVisibleSize()

enum{
	BTN_QUEDING, //确定按钮
	BTN_QUXIAO,	 //取消按钮
};

GNS_YJFLayer::GNS_YJFLayer()
{
	//m_PopWndNode = 0;
}

GNS_YJFLayer::~GNS_YJFLayer()
{

}

GNS_YJFLayer* GNS_YJFLayer::createLayer(std::string CsbName)
{
	auto pLayer = GNS_YJFLayer::create();
	auto pScene = Director::getInstance()->getRunningScene();
	if (!pScene)
		return 0;
	return pLayer;
}

Node* GNS_YJFLayer::Loadcsb(std::string csbName)
{
	auto node = CSLoader::createNode(csbName);
	node->setContentSize(winsize);
	node->ignoreAnchorPointForPosition(false);
	node->setAnchorPoint(Vec2(0.5,0.5));
	ui::Helper::doLayout(node);
	return node;
}

bool GNS_YJFLayer::init()
{
	if (!Layer::init())
	{
		return false;
	}
    /*     */
	auto callback = [](Touch *, Event *)
	{
		return true;
	};
	listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = callback;
	listener->setSwallowTouches(false);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
	return true;

    return true;
}

 void GNS_YJFLayer::onExit()
{
    Director::getInstance()->getTextureCache()->removeUnusedTextures();
    Layer::onExit();
}

void GNS_YJFLayer::setSwallow( bool isShow)
{
	/*if (isShow)
	{
		listener->setSwallowTouches(true);
		//m_PopWndNode->stopAllActions();
		//m_PopWndNode->setPosition(Vec2(winsize.width / 2, winsize.height*1.2));
		//auto NodeMove = MoveTo::create(0.5f, Vec2(winsize.width / 2, winsize.height / 2));
		//auto  easeBounceOut = EaseBounceOut::create(NodeMove);
		//m_PopWndNode->runAction(easeBounceOut);
	}
	else
	{
		//auto NodeMove = MoveTo::create(0.5f, Vec2(winsize.width / 2, winsize.height*1.5));
		listener->setSwallowTouches(false);
		//m_PopWndNode->runAction(NodeMove);
	}*/
   // listener->setSwallowTouches(isShow);
}

void GNS_YJFLayer::ShowMyTips(string str, bool isShow)
{
	if (isShow)
	{
		ShowLabel(str);
		m_TipsBg->setPosition(Vec2(winsize.width / 2, winsize.height*1.2));
		auto NodeMove = MoveTo::create(0.5f, Vec2(winsize.width / 2, winsize.height / 2));
		auto  easeBounceOut = EaseBounceOut::create(NodeMove);
		m_TipsBg->runAction(easeBounceOut);
	}
	else
	{
		auto NodeMove = MoveTo::create(0.5f, Vec2(winsize.width / 2, winsize.height*1.5));
		m_TipsBg->runAction(NodeMove);
	}
	
}

void GNS_YJFLayer::TextFileTouch(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
	auto imputText = (TextField*)pSender;
	if (type == ui::Widget::TouchEventType::BEGAN)
	{
		if (imputText->getPlaceHolder().size()!=0)
		{
			m_tips = imputText->getPlaceHolder();
		}
		imputText->setPlaceHolder("");
	}
}

Button* GNS_YJFLayer::crateBtnWithFile(string fileName,int tag,Vec2 point)
{
	auto btn = Button::create(fileName, fileName, fileName);
	btn->setTag(tag);
	btn->ignoreAnchorPointForPosition(false);
	btn->setAnchorPoint(Vec2(0.5f,0.5f));
	btn->setPosition(point);
	return btn;
}

void GNS_YJFLayer::initTipsWnd(string tipsBgName,string title,Color3B titleColor,Color3B labelColor/*正文颜色*/,float titleFSize,float labelFSize)
{
	createTipsBgWithFile(tipsBgName);
	initTitle(title, Color3B(255, 255, 255), 30);
	if (m_label == nullptr)
	{
		m_label = Label::createWithSystemFont("", "", 30);
			//createLabel("", Vec2(m_WndViewSize.width / 2, m_WndViewSize.height - labelFSize * 2), labelColor, labelFSize);
		m_TipsBg->addChild(m_label, 1000);
	};
}

void GNS_YJFLayer::createTipsBgWithFile(string fileName)
{
	m_TipsBg = Sprite::create(fileName);
	addChild(m_TipsBg, 1000);
	m_WndViewSize = m_TipsBg->getContentSize();
	m_TipsBg->setPosition(Vec2(winsize.width / 2, winsize.height*1.5));
}

GNS_CursorTextField * GNS_YJFLayer::createEditBox(std::string tips, Point pos, float FontSize, Color3B color, int limit, bool encryption)
{
	GNS_CursorTextField* editbox = GNS_CursorTextField::textFieldWithPlaceHolder(tips.c_str(), "", 24);
	editbox->setColor(color);
	editbox->setAnchorPoint(Vec2(0, 0.5f));
	editbox->setPosition(pos);
	editbox->setHorizontalAlignment(TextHAlignment::LEFT);
	editbox->initCursorSprite(FontSize, color);
	editbox->setLimitNum(limit);
	editbox->setPasswordEnabled(encryption);
	return editbox;
}

Label* GNS_YJFLayer::createLabel(string str,Vec2 point,Color3B color,float fontSize)
{
	auto label = Label::createWithSystemFont(str, "", fontSize);
	label->setString(str.c_str());
	label->setPosition(point);
	label->setLineBreakWithoutSpace(true);
	label->setColor(color);
	label->setAlignment(TextHAlignment::CENTER);
	return label;
}

void GNS_YJFLayer::initTitle(string str,Color3B color,float fontsize)
{
	if (m_Title==nullptr)
	{
		m_Title = createLabel(str, Vec2(m_WndViewSize.width / 2, m_WndViewSize.height - fontsize * 2), color, fontsize);
		m_TipsBg->addChild(m_Title, 1000);
	}
}

void GNS_YJFLayer::ShowLabel(string str, Color3B color, float fontsize, bool isShow)
{
	m_label->setString(str.c_str());
}

void GNS_YJFLayer::ShowCommonTips(std::string tips, bool isShow)
{
	/*if (!m_TipsLayer)
	{
		m_TipsLayer = GNS_TipsWnd::createLayer("MainScenes/Layer.csb");
	}
	m_TipsLayer->ShowTips(tips, isShow);*/
}